package com.senseauto.aicamera;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.util.Size;
import android.widget.EditText;
import android.widget.Toast;

import com.senseauto.aicamera.preview.Preview_Detector;

import java.util.ArrayList;
import java.util.List;

/**
 * 这个工程里的代码是从 app 工程拷贝过来的，只是改了一个应用包名而已，改成了和响应包名一样的
 */
public class MainActivity extends AppCompatActivity {
    String TAG = "MainActivity";
    private String[] permissions = new String[]{Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.RECORD_AUDIO};
    private List<String> mPermissionList = new ArrayList<>();
    private int PERMISSION_REQUEST_CODE = 100;
    EditText etCameraId,etWidth,etHeight;

    private boolean hasAllPermission() {
        mPermissionList.clear();
        if (Build.VERSION.SDK_INT < 23) {
            return true;
        }
        for (int i = 0; i < permissions.length; i++) {
            if (ContextCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
                mPermissionList.add(permissions[i]);
            }
        }
        return mPermissionList.size() <= 0;

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        boolean hasPermissionDismiss = false;
        if (requestCode == PERMISSION_REQUEST_CODE) {
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[i] == -1) {
                    hasPermissionDismiss = true;
                }
            }

            if (hasPermissionDismiss) {
                Toast.makeText(this, "请授予必要权限", Toast.LENGTH_SHORT).show();

            } else {
                printCameraInfo();
            }
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etCameraId = findViewById(R.id.etCameraId);
        etWidth = findViewById(R.id.etWidth);
        etHeight = findViewById(R.id.etHeight);
        if (!hasAllPermission()) {
            Log.e(TAG, "requestPermissions");
            ActivityCompat.requestPermissions(MainActivity.this, permissions, PERMISSION_REQUEST_CODE);
        }else{ //第一次赋值过权限后，第二次打开界面
            //startCamera();
            printCameraInfo();
        }
        findViewById(R.id.go1).setOnClickListener(view->{
            Intent intent = new Intent(getApplicationContext(), CameraActivity.class);
            startActivity(intent);
        });
        findViewById(R.id.go).setOnClickListener(view->{
            Intent intent = new Intent(getApplicationContext(), Camera2MediaRecorderActivity.class);
            //intent.putExtra("dmsCameraID", "5");
            String cameraId =etCameraId.getText().toString();
            String width =etWidth.getText().toString();
            String height =etHeight.getText().toString();
            intent.putExtra("dmsCameraID", TextUtils.isEmpty(cameraId.trim())? "2": cameraId.trim());
            intent.putExtra("width",  TextUtils.isEmpty(width.trim())? "1920": width.trim());
            intent.putExtra("height",  TextUtils.isEmpty(height.trim())? "1080": height.trim());
            startActivity(intent);
        });
        findViewById(R.id.goCamera2Preview).setOnClickListener(view->{
            Intent intent = new Intent(getApplicationContext(), Camera2PreviewActivity.class);
            //intent.putExtra("dmsCameraID", "5");
            String cameraId =etCameraId.getText().toString();
            String width =etWidth.getText().toString();
            String height =etHeight.getText().toString();
            intent.putExtra("dmsCameraID", TextUtils.isEmpty(cameraId.trim())? "2": cameraId.trim());
            intent.putExtra("width",  TextUtils.isEmpty(width.trim())? "1920": width.trim());
            intent.putExtra("height",  TextUtils.isEmpty(height.trim())? "1080": height.trim());
            startActivity(intent);
        });

        findViewById(R.id.goProxy).setOnClickListener(view->{
            Intent intent = new Intent(getApplicationContext(), CameraProxyActivity.class);
            //intent.putExtra("dmsCameraID", "5");
            String cameraId =etCameraId.getText().toString();
            String width =etWidth.getText().toString();
            String height =etHeight.getText().toString();
            intent.putExtra("dmsCameraID", TextUtils.isEmpty(cameraId.trim())? "2": cameraId.trim());
            intent.putExtra("width",  TextUtils.isEmpty(width.trim())? "1920": width.trim());
            intent.putExtra("height",  TextUtils.isEmpty(height.trim())? "1080": height.trim());
            startActivity(intent);
        });

        findViewById(R.id.goTest).setOnClickListener(view->{
            startActivity(new Intent(getApplicationContext(), TestActivity.class));
        });
        findViewById(R.id.goPreview).setOnClickListener(view->{
            startActivity(new Intent(getApplicationContext(), Preview_Detector.class));
        });
        findViewById(R.id.goGlSurfaceView).setOnClickListener(view->{
            startActivity(new Intent(getApplicationContext(), GLSurfaceViewActivity.class));
        });

        findViewById(R.id.goVideoRecorder).setOnClickListener(view->{
            Intent intent = new Intent(getApplicationContext(), VideoRecord2Activity.class);
            //intent.putExtra("dmsCameraID", "5");
            String cameraId =etCameraId.getText().toString();
            String width =etWidth.getText().toString();
            String height =etHeight.getText().toString();
            intent.putExtra("dmsCameraID", TextUtils.isEmpty(cameraId.trim())? "2": cameraId.trim());
            intent.putExtra("width",  TextUtils.isEmpty(width.trim())? "1920": width.trim());
            intent.putExtra("height",  TextUtils.isEmpty(height.trim())? "1080": height.trim());
            startActivity(intent);
        });
        findViewById(R.id.goSwitchCamera).setOnClickListener(view->{
            Intent intent = new Intent(getApplicationContext(), Camera2MutilPreviewActivity.class);

            String cameraId =etCameraId.getText().toString();
            String width =etWidth.getText().toString();
            String height =etHeight.getText().toString();
            intent.putExtra("dmsCameraID", TextUtils.isEmpty(cameraId.trim())? "2": cameraId.trim());
            intent.putExtra("width",  TextUtils.isEmpty(width.trim())? "1920": width.trim());
            intent.putExtra("height",  TextUtils.isEmpty(height.trim())? "1080": height.trim());
            startActivity(intent);
        });
    }
    private void printCameraInfo() {
        CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        try {
            Log.e(TAG, "Camera manager.getCameraIdList()=>" + (cameraManager.getCameraIdList() == null ? -1 : cameraManager.getCameraIdList().length));
            for (String cameraId : cameraManager.getCameraIdList()) {
                Log.e(TAG, "Camera for 循环里：cameraId=" + cameraId);
                CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
                StreamConfigurationMap configMap = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
                Size[] sizes = configMap.getOutputSizes(SurfaceTexture.class);
                // 打印输出所有分辨率信息
                for (Size size : sizes) {
                    Log.e(TAG, "Camera " + cameraId + " supported resolution: " + size.getWidth() + "x" + size.getHeight());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}